const Koa = require('koa'),
    koa = require('koa-router')(),
    json = require('koa-json'),
    logger = require('koa-logger'),
    auth = require('./routes/auth.js'),
    api = require('./routes/api.js'),
    jwt = require('koa-jwt'),
    path = require('path'),
    serve = require('koa-static'),
    historyApiFallback = require('koa2-history-api-fallback');

const app = new Koa();

app.use(require('koa-bodyparser')());
app.use(json());
app.use(logger());

app.use(async function(ctx, next) {
    let start = new Date;
    await next();
    let ms = new Date - start;
    console.log('%s %s - %s', ctx.method, ctx.url, ms);
});

app.use(async function(ctx, next) { //  如果JWT验证失败，返回验证失败信息
    try {
        await next();
    } catch (err) {
        if (401 == err.status) {
            ctx.status = 401;
            ctx.body = {
                success: false,
                token: null,
                info: 'Protected resource, use Authorization header to get access'
            };
        } else {
            throw err;
        }
    }
});

app.on('error', function(err, ctx) {
    console.log('server error', err);
});

koa.use('/auth', auth.routes()); // 挂载到koa-router上，同时会让所有的auth的请求路径前面加上'/auth'的请求路径。
koa.use("/api", jwt({ secret: 'vue-koa-demo' }), api.routes()); // 所有走/api/打头的请求都需要经过jwt验证。

app.use(koa.routes()); // 将路由规则挂载到Koa上。
app.use(historyApiFallback());
app.use(serve(path.resolve('../dist'))); // 将webpack打包好的项目目录作为Koa静态文件服务的目录
app.listen(3001, () => {
    console.log('Koa  listening  3001');
});
module.exports = app;